package com.jzo2o.foundations.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.wu
 * @Date 2025/1/16 15:41
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Autowired
    private IServeItemService serveItemService;
    @Autowired
    private IRegionService regionService;
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        //调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO, () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    @Override
    public void add(List<ServeUpsertReqDTO> serveUpsertReqList) {
        serveUpsertReqList.forEach(serveUpsertReqDTO -> {
            ServeItem byId = serveItemService.getById(serveUpsertReqDTO.getServeItemId());
            if (ObjectUtil.isEmpty(byId)){
                throw new ForbiddenOperationException("服务项不存在不能添加");
            }
            if (byId.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("服务项未启动不能添加");
            }
            Integer count = this.lambdaQuery().eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .count();
            if (count>0){
                throw new ForbiddenOperationException("服务项已存在不能添加");
            }
            String cityCode = regionService.getById(serveUpsertReqDTO.getRegionId()).getCityCode();
            Serve serve = BeanUtils.copyProperties(serveUpsertReqDTO, Serve.class);
            serve.setCityCode(cityCode);
            boolean save = this.save(serve);
            if (!save){
                throw new ForbiddenOperationException("添加失败");
            }
        });
    }

    @Override
    public void updatePrice(Long id, BigDecimal price) {
        Serve byId = getById(id);
        if (ObjectUtil.isEmpty(byId)){
            throw new ForbiddenOperationException("服务不存在");
        }
        byId.setPrice(price);
        boolean update = this.lambdaUpdate().set(Serve::getPrice, price).eq(Serve::getId, id).update();
        if (!update){
            throw new ForbiddenOperationException("修改失败");
        }
    }

    @Override
    public void onSale(Long id) {
        //1.判断服务是否存在
        Serve byId =getById(id);
        if (ObjectUtil.isEmpty(byId)){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.判断是否有区域
        if (ObjectUtil.isEmpty(byId.getRegionId())){
            throw new ForbiddenOperationException("服务未关联区域");
        }
        //3.判断服务是否上架过
        if (byId.getSaleStatus()== FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务已上架");
        }
        //4.判断服务项是否存在
        ServeItem serveItem= serveItemService.getById(byId.getServeItemId());
        if (ObjectUtil.isEmpty(serveItem)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //5.判断服务项是否已启动
        if (serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启动");
        }
        //5.修改状态为已上架
        boolean update = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus()).eq(Serve::getId, id).update();
        if (!update){
            throw new ForbiddenOperationException("修改失败");
        }
    }

    @Override
    public void delete(Long id) {
        Serve item =getById(id);
        if (ObjectUtil.isEmpty(item)){
            throw new ForbiddenOperationException("服务不存在");
        }
        if (item.getSaleStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务启用中");
        }
        boolean remove = this.removeById(id);
        if (!remove){
            throw new ForbiddenOperationException("删除失败");
        }
    }

    @Override
    public void offSale(Long id) {
        //1.判断服务是否存在
        Serve byId =getById(id);
        if (ObjectUtil.isEmpty(byId)){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.判断是否有区域
        if (ObjectUtil.isEmpty(byId.getRegionId())){
            throw new ForbiddenOperationException("服务未关联区域");
        }
        //3.判断服务是否下架过
        if (byId.getSaleStatus()== FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("服务已下架");
        }
        //4.判断服务项是否存在
        ServeItem serveItem= serveItemService.getById(byId.getServeItemId());
        if (ObjectUtil.isEmpty(serveItem)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //5.判断服务项是否已启用
        if (serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启动");
        }
        //5.修改状态为已下架
        boolean update = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus()).eq(Serve::getId, id).update();
        if (!update){
            throw new ForbiddenOperationException("修改失败");
        }
    }

    @Override
    public void onHot(Long id) {
        //1.判断服务是否存在
        Serve byId =getById(id);
        if (ObjectUtil.isEmpty(byId)){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.判断是否有区域
        if (ObjectUtil.isEmpty(byId.getRegionId())){
            throw new ForbiddenOperationException("服务未关联区域");
        }
        //3.判断服务是否已设置热门
        if (byId.getIsHot()== FoundationStatusEnum.ISNOT.getStatus()){
            throw new ForbiddenOperationException("服务已热门");
        }
        //4.判断服务项是否存在
        ServeItem serveItem= serveItemService.getById(byId.getServeItemId());
        if (ObjectUtil.isEmpty(serveItem)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //5.判断服务项是否已启用
        if (serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启动");
        }
        //5.修改状态为热门
        boolean update = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ISNOT.getStatus()).eq(Serve::getId, id).update();
        if (!update){
            throw new ForbiddenOperationException("修改失败");
        }
    }

    @Override
    public void offHot(Long id) {
        //1.判断服务是否存在
        Serve byId =getById(id);
        if (ObjectUtil.isEmpty(byId)){
            throw new ForbiddenOperationException("服务不存在");
        }
        //2.判断是否有区域
        if (ObjectUtil.isEmpty(byId.getRegionId())){
            throw new ForbiddenOperationException("服务未关联区域");
        }
        //3.判断服务是否已非热门
        if (byId.getIsHot()== FoundationStatusEnum.NOHOT.getStatus()){
            throw new ForbiddenOperationException("服务已非热门");
        }
        //4.判断服务项是否存在
        ServeItem serveItem= serveItemService.getById(byId.getServeItemId());
        if (ObjectUtil.isEmpty(serveItem)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //5.判断服务项是否已启用
        if (serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启动");
        }
        //5.修改状态为非热门
        boolean update = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.NOHOT.getStatus()).eq(Serve::getId, id).update();
        if (!update){
            throw new ForbiddenOperationException("修改失败");
        }
    }
}
